package tk.heimashuma.store.utils;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

@Configuration
@Log4j2
public class IpConfiguration implements ApplicationListener<WebServerInitializedEvent> {
    private static WebServerInitializedEvent event;
    @Override
    public void onApplicationEvent(WebServerInitializedEvent event) {
        IpConfiguration.event = event;
    }

    public static  int getPort(){
        int port = -1;
        if(event != null)
            port = IpConfiguration.event.getWebServer().getPort();
        if(port == -1){
            log.error("获取端口失败，返回默认端口8080");
            return 8080;
        }
        return port;
    }
}
